//
//  ZYComponentLoader.m
//  DesignMode
//
//  Created by wangzhipan on 2024/11/7.
//
#import "ZYComponentLoader.h"
#import "ZYCompositeImp.h"
#import "ZYOnlineComRegister.h"
static NSString *const kComponentRootName = @"zy_root_component";

@interface ZYComponentLoader()

@end

@implementation ZYComponentLoader
- (instancetype)initWithRootView:(UIView *)rootView
{
    if (self = [super init]) {
        self.rootComponent = [[ZYComponent alloc] initWithRootView:rootView];
    }
    return self;
}

- (NSArray <id<IZYComponentRegister>> *)defaultRegisters {
    return @[
        [[ZYOnlineComRegister alloc] initWithComponentId:@"onLineList" priority:1],
        [[ZYOnlineComRegister alloc] initWithComponentId:@"onLineUser" priority:3],
        [[ZYOnlineComRegister alloc] initWithComponentId:@"onLineXXX" priority:0],
    ];
}

- (void)doLoadAllComponent
{
    NSArray <id<IZYComponentRegister>> *sortList = [self.defaultRegisters sortedArrayUsingComparator:^NSComparisonResult(id<IZYComponentRegister> obj1, id<IZYComponentRegister> obj2) {
        return obj1.priority - obj2.priority;
    }];
    id<IComponent, IComponentLifecycle> rootComponent = (id<IComponent, IComponentLifecycle>)self.rootComponent;
    for (id<IZYComponentRegister> comRegister in sortList) {
        id<IComponent, IComponentLifecycle> component = (id<IComponent, IComponentLifecycle>)[comRegister generateComponent];
        [self.rootComponent addComponent:component];
    }
    [rootComponent componentWillLoad];
    [rootComponent componentDidLoad];
}

- (void)doUnloadAllDomponent
{
    id<IComponent, IComponentLifecycle> rootComponent = (id<IComponent, IComponentLifecycle>)self.rootComponent;
    [rootComponent componentWillUnload];
    [rootComponent componentDidUnload];
}

@end
